const db = require("../models");
const { Op } = require("sequelize");
const { course } = require("../models");
const Course = db.course;
const Tech = db.tech;

exports.searchCourseName = async(req, res) => {
    try{
        let ret_val = [];
        await Course.findAll({
            where: { 
                course_name: { [Op.like]: '%' + req.query.course_name + '%' }
            },
            raw: true,
            limit: 100
        })
        .then((data) => {
            ret_val = data;
        })
        
        for(let i=0;i<ret_val.length;i++){
            await Tech.findOne({
                where: {
                    tech_id : ret_val[i].tech_id
                },
                raw: true
            }).then((data) => {
                ret_val[i].tech_name = data.tech_name
            })
        }

        res.setHeader("Access-Control-Allow-Origin", '*')
        res.send(ret_val);
    } catch (err){
        res.status(500).send({
            message: err.message || "搜索失败",
        });
    }
}

exports.searchTechName = async(req, res) => {
    try{
        let ret_val = [];
        let tech_list = [];
        await Tech.findAll({
            where: { 
                tech_name: { [Op.like]: '%' + req.query.tech_name + '%' }
            },
            raw: true,
            limit: 100
        })
        .then((data) => {
            tech_list = data;
        })
        
        for(let i=0;i<tech_list.length;i++){
            await Course.findAll({
                where: {
                    tech_id : tech_list[i].tech_id
                },
                raw: true
            }).then((data) => {
                for(let j=0;j<data.length;j++){
                    ret_val.push(data[j]);
                    ret_val[ret_val.length-1].tech_name = tech_list[i].tech_name
                }
            })
        }

        res.setHeader("Access-Control-Allow-Origin", '*')
        res.send(ret_val);
    } catch (err){
        res.status(500).send({
            message: err.message || "搜索失败",
        });
    }
}

exports.searchTechId = async(req, res) => {
    try{
        let ret_val = [];
        await Course.findAll({
            where: { 
                tech_id: req.query.tech_id
            },
            raw: true
        })
        .then((data) => {
            ret_val = data;
        })
        
        for(let i=0;i<ret_val.length;i++){
            await Tech.findOne({
                where: {
                    tech_id : ret_val[i].tech_id
                },
                raw: true
            }).then((data) => {
                ret_val[i].tech_name = data.tech_name
            })
        }

        res.setHeader("Access-Control-Allow-Origin", '*')
        res.send(ret_val);
    } catch (err){
        res.status(500).send({
            message: err.message || "搜索失败",
        });
    }
}

exports.findAll = (req, res) => {
    Course.findAll({ })
        .then((data) => {
            res.setHeader("Access-Control-Allow-Origin", '*')
            res.send(data);
        })
        .catch((err) => {
            res.status(500).send({
                message: err.message || "获取列表失败",
            });
        });
}